package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.service.BizPayableService;
import com.mes.biz.service.BizPaymentBillService;
import com.mes.biz.service.BizPaymentDetailService;
import com.mes.biz.service.BizPaymentService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.framework.cache.DictUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 收款单Controller
 * @author wy
 * @email wy
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/biz/bizPayment")
public class BizPaymentController extends BaseController {

    @Autowired
    private BizPaymentService bizPaymentService;
    @Autowired
    private BizPaymentDetailService bizPaymentDetailService;
    @Autowired
    private BizPaymentBillService bizPaymentBillService;
    /**
     * 收款单列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizPayment bizPayment, HttpServletRequest request, HttpServletResponse response) {
        bizPayment.setPage(new PageDomain(request, response));
        return R.data(bizPaymentService.findPage(bizPayment));
    }

    /**
     * 新增收款单
     */
    @Log(title = "新增收款单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizPayment bizPayment) {
        return R.status(bizPaymentService.createPayment(bizPayment));
    }

    /**`
     * 修改收款单
     */
    @Log(title = "修改收款单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizPayment bizPayment) {
        return R.status(bizPaymentService.updatePayment(bizPayment));
    }

    /**
     * 删除收款单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizPayment:remove')")
    @Log(title = "删除收款单", businessType = BusinessType.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable String id) {
        return R.status(bizPaymentService.deletePayment(id));
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @PreAuthorize("@ss.hasPermi('biz:bizPayment:query')")
    @GetMapping(value = "/{id}")
    public R<BizPayment> detail(@PathVariable("id") String id) {
        BizPayment bizPayment = bizPaymentService.get(id);
        List<BizPaymentDetail> paymentDetails = bizPaymentDetailService.selectByPaymentId(id);
        bizPayment.setPaymentDetailList(paymentDetails);
        List<BizPaymentBill> bizPaymentBills = bizPaymentBillService.selectByPaymentId(id);
        bizPayment.setPaymentBillList(bizPaymentBills);
        return R.data(bizPayment);
    }

    @PreAuthorize("@ss.hasPermi('biz:bizPayment:audit')")
    @Log(title = "审核收款单", businessType = BusinessType.DELETE)
    @GetMapping(value = "/audit/{id}")
    public R auditPurchase(@PathVariable String id) {
        return R.data(bizPaymentService.auditPayment(id));
    }
}
